/*
 * @Description:  包含我的位置、地图选点按钮组件
 * @Author: buwei.huang
 * @Date: 2019-07-12 14:18:59
 * @Last Modified by: buwei.huang
 * @Last Modified time: 2019-09-10 15:52:32
 */
<template>
  <div
    class="btn_wrap flex-h"
    v-show="isShow"
  >
    <div
      class="btn_item btn_left flex-h flex-vc flex-he"
      @click="selectMySite"
    >
      <i class="icon iconfont icon-mypoint baseColorNoAcitve fontsize44" />
      <span class="fontsize28">我的位置</span>
    </div>
    <div
      class="btn_item btn_right flex-h flex-vc"
      @click="goToPage"
    >
      <i class="iconColor icon iconfont icon-dituxuandian fontsize44" />
      <span class="fontsize28">地图选点</span>
    </div>
  </div>
</template>
<script>
import { getCurrentPosition } from 'vue-xiaobu-utils'
import { Indicator } from 'mint-ui'
export default {
  props: {
    routeType: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      pos: {},
      isShow: false
    }
  },
  mounted() {
    this._getCurrentPosition()
    this.$EventBus.$on('isShow', () => {
      this.isShow = true
    })
    // window.onResume = () => {
    //   this._getCurrentPosition()
    // }
  },
  methods: {
    goToPage() {
      this.$AppRouterUtils.open('mapChooseSite.html?_tv=true&_rv=false&ROUTETYPE=' + this.routeType)
    },
    selectMySite() {
      // console.log(this.pos)
      if (this.pos.add) {
        this.$AppRouterUtils.goBack(this.routeType + 'NAME=' + this.pos.add + '&LONGITUDE=' + this.pos.longitude + '&LATITUDE=' + this.pos.latitude)
      } else {
        this._getCurrentPosition()
      }
    },
    async _getCurrentPosition() { // 获取当前gps位置
      try {
        Indicator.open()
        let pos = await getCurrentPosition(this)
        // 更新gps数据
        this.pos = pos
        // alert(JSON.stringify(this.pos) + '')
      } catch (err) {
        this.$Toast.then(Toast => {
          Toast.showShortBottom(
            '获取定位失败',
            () => {
              console.log('1111')
            }
          )
        })
        console.log('获取定位失败1')
      } finally {
        Indicator.close()
      }
    }
  }
}
</script>
<style>
.btn_wrap {
  height: 112px;
  background-color: #fff;
}
.btn_item {
  width: 50%;
  height: 100%;
}
.btn_item .icon {
  margin-right: 12px;
}
.btn_left {
  padding-right: 77px;
}
.btn_right {
  padding-left: 77px;
}
.btn_wrap .iconColor {
  color: #f5a623;
}
</style>
